package com.lge.clock.alarmclock;
 

import android.content.Context;
import android.os.PowerManager;
import android.util.Log;

/**
 * Hold a wakelock that can be acquired in the AlarmReceiver and
 * released in the AlarmAlert activity
 * @author LGE Cappuccino
 */ 
public class AlarmAlertWakeLock {
    private static PowerManager.WakeLock sScreenWakeLock = null;
    private static PowerManager.WakeLock sCpuWakeLock = null;
    private static final String TAG	= "AlarmAlertWakeLock"; 
    
    /**
     * Method acquireCpuWakeLock.
     * @param context Context
     */
    public static void acquireCpuWakeLock(Context context) {
        Log.v(TAG, "Acquiring cpu wake lock");
        
        //if (sCpuWakeLock != null) {
        if (sCpuWakeLock == null) {
//            return;
    //    }

        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);

        sCpuWakeLock = pm.newWakeLock(
                PowerManager.PARTIAL_WAKE_LOCK |
                PowerManager.ACQUIRE_CAUSES_WAKEUP |
                PowerManager.ON_AFTER_RELEASE, "ALStart");
                sCpuWakeLock.setReferenceCounted(false);
        }
        
            if (!sCpuWakeLock.isHeld()){
                Log.d(TAG, "ACQUIRE_CAUSES_WAKEUP is set in AlarmAlertWakeLock.java acquireCpuWakeLock");
                sCpuWakeLock.acquire();
            }
	}

    /**
     * Method acquireScreenWakeLock.
     * @param context Context
     */
    public static void acquireScreenWakeLock(Context context) {
    	Log.v(TAG, "Acquiring screen wake lock");
    	
        //if (sScreenWakeLock != null) {
            if (sScreenWakeLock == null) {
                 Log.v(TAG, "sScreenWakeLock == null ");
           // Log.v(TAG, "sScreenWakeLock != null ===> return ");
     //       return;
//        }

            PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);

            sScreenWakeLock = pm.newWakeLock(
                    PowerManager.FULL_WAKE_LOCK |
                    PowerManager.ACQUIRE_CAUSES_WAKEUP , "ALStart");
            sScreenWakeLock.setReferenceCounted(false);
            
            }
            
            if (!sScreenWakeLock.isHeld()){
                Log.d(TAG, "ACQUIRE_CAUSES_WAKEUP is set in AlarmAlertWakeLock.java acquireScreenWakeLock");
                sScreenWakeLock.acquire();     
            }       
        
	}

    /**
     * Method release.
     */
    public static void release() {
    	Log.v(TAG, "Releasing wake lock");
    	
        if (sCpuWakeLock != null) {
            sCpuWakeLock.release();
            sCpuWakeLock = null;
        }
        //if (sScreenWakeLock != null) {
            if (sScreenWakeLock.isHeld())
                sScreenWakeLock.release();
            //sScreenWakeLock = null;
        //}
    }
}